﻿using System;
using System.Threading;
using OtherDevice;
using System.ComponentModel;
namespace OtherDevice
{
    [Description("松下测高")]
    public class LaserPanasonic : LaserBase
    {
        public LaserPanasonic(string strName) : base(strName)
        {
            controlFrom = new Form_测高();
        }
        public override bool Get(int n_取值次数, int n_间隔时间, ref double pos)
        {
            try
            {
                string str = $"%{1:D2}#RMD**\r";
                lock (Lock)
                {
                    double dSum = 0;
                    for (int i = 0; i < n_取值次数; i++)
                    {
                        byte[] byteRe = new byte[15];
                        if (SendAndReciveData(str, ref byteRe, 500))
                        {
                            str = System.Text.Encoding.Default.GetString(byteRe);
                            string[] strs = str.Split('D');
                            string info = strs[1].Substring(0, 4);
                            info += "." + strs[1].Substring(4, 4);
                            dSum += double.Parse(info);
                        }
                        else
                        {
                            return false;
                        }
                    }
                    pos = dSum / (double)n_取值次数;
                    return true;
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show($"高度获取失败{ex}");
                return false;
            }
        }
    }
}